package com.example.interview.example.bankCallSystem.impl_3;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class MainTest {
    public static void main(String[] args) {

        final ExecutorService threadPool = Executors.newCachedThreadPool();
        // 每个队伍 最多有10人  //之所以要生成一个Producer,是因为这里不能加static，否则无法调用基类的底层方法(很多不是static的，而你又不能改！！！)
        final ArrayBlockingQueue<Consumer> normals = new ArrayBlockingQueue<>(10);
        final ArrayBlockingQueue<Consumer> vips = new ArrayBlockingQueue<>(10);
        final ArrayBlockingQueue<Consumer> quicks = new ArrayBlockingQueue<>(10);

        //step1: init客户
        final Producer producer = new Producer();
        producer.setNormals(normals);
        producer.setQuicks(quicks);
        producer.setVips(vips);
        //向不同type的客户队列中添加init客户
        threadPool.execute(() -> producer.produce()); //子线程生成客户，等待取用，模拟比例： VIP客户 ：快速客户 ：普通客户 =  1 ：3 ：6

        //step2: //初始化 1~6号窗口
        final WindowList Windows = new WindowList();

        //step3: 调度器: 融合 window + 客户，构成业务办理
        final Dispatcher dispatcher=new Dispatcher();

        //(获取空闲的window,让当前的customer办理业务，当前[type1的队列]没人就到[type2的队列]找人，以此类推...)
        threadPool.execute(() -> dispatcher.doQuick(threadPool, Windows, vips, normals, quicks));
        threadPool.execute(() -> dispatcher.doVIP(threadPool, Windows, vips, normals, quicks));
        threadPool.execute(() -> dispatcher.doNomal(threadPool, Windows, vips, normals, quicks));

        //step4: 任务通道已打通完成，做以总结：
        threadPool.execute(() -> {
            try {
                while (true) {
                    // http://blog.csdn.net/yingzishizhe/article/details/8769907
                    int threadCount = ((ThreadPoolExecutor) threadPool).getActiveCount();
                    System.out.println("现在活跃的线程数量为: " + threadCount);//当前工作的线程:5(五个.execute()执行线程的方法)
                    System.out.println("现在排队的人数为:" + (vips.size() + normals.size() + quicks.size()));
                    Thread.sleep(3000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }
}
